package cc.linker.steplink.crm.merchanth5.controller;

import cc.linker.steplink.crm.commons.Constants;
import cc.linker.steplink.crm.commons.domain.WxUser;
import cc.linker.steplink.crm.merchanth5.commons.RequestFilter;
import cc.linker.steplink.crm.merchanth5.service.DongGoodsService;
import cc.linker.steplink.crm.response.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 懂小姐业务
 * @Author: wuhangfeng
 * @Date: Created in 2019/11/14 13:29
 * @Version: 1.11.6
 */
@RestController
public class DongGoodsController {

    @Autowired
    private DongGoodsService dongGoodsService;

    @Autowired
    private RequestFilter requestFilter;

    /**
     * 创建订单
     *
     * @param addressId       地址id
     * @param goodsId         商品id(分小b商品和大b商品)
     * @param goodsNum        商品数量
     * @param skuId           skuId
     * @param shoppingCartIds 购物车id
     * @param orderSn         订单sn
     * @param distributor     分享人id
     * @param score           是否使用积分(0-不使用，1-使用)(使用即为全部使用，但不超过订单价格)
     * @param remark          备注
     */
    @PostMapping("/h5/dong/createorder")
    public Map createOrder(@RequestBody Map<String, Object> param, HttpServletRequest request) throws GlobalException {
        // 创建订单，生成返润体系，然后调中金得到支付签名，返回给前端
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        String orgId = wxUser.getOrgId();
        param.put("customerId", customerId);
        param.put("orgId", orgId);
        return dongGoodsService.createOrder(param);
    }

    /**
     * 订单回调
     *
     * @param orderSn 订单sn
     * @param status  状态//10处理中，20成功；30失败；40交易关闭；50未支付
     */
    @PostMapping("/dong/paycallback")
    public void payCallback(@RequestBody Map<String, Object> param) throws Exception {
        String transactionId = (String) param.get("transactionId");//商户订单号
        String outTradeNo = (String) param.get("outTradeNo");//商户订单号
        String totalFee = param.get("totalFee")==null?null:param.get("totalFee").toString();//订单金额
        String orderSn = param.get("orderSn")==null?null:param.get("orderSn").toString();//订单金额
        String status = param.get("status")==null?null:param.get("status").toString();//订单金额
        String txsn = param.get("txsn")==null?null:param.get("txsn").toString();//订单金额
        if(orderSn==null){
            orderSn = outTradeNo;
        }
        if(status==null){
            status = "20";
        }
        if(txsn==null){
            txsn = transactionId;
        }
        // todo 比对金额
        Map<String, Object> map = new HashMap<>();
        map.put("orderSn", orderSn);
        map.put("txsn", txsn);
        map.put("status", status);
        if (!map.containsKey("orderSn") || !map.containsKey("status")) {
            throw GlobalException.build("缺少参数");
        }
        // 回调
        dongGoodsService.payCallback(map);
    }

    /**
     * 确认订单
     *
     */
    @PostMapping("/h5/dong/checkShoppingOrder")
    public Object checkOrder(@RequestBody Map<String, Object> param, HttpServletRequest request) throws GlobalException {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        param.put("customerId", customerId);
        String orgId = wxUser.getOrgId();
        param.put("orgId", orgId);
        return dongGoodsService.checkOrder(param);
    }
}
